#include <iostream>
#include <BinaryTree.hpp>
#include <vector>
using namespace std;

class Solution
{
public:
    vector<TreeNode *> generateTrees(int n)
    {
        vector<vector<vector<TreeNode *>>> dp(n, vector<vector<TreeNode *>>(n, vector<TreeNode *>(0)));
        return generateTrees(0, n - 1, n, dp);
    }

    vector<TreeNode *> generateTrees(int start, int end, int len, vector<vector<vector<TreeNode *>>> &dp)
    {
        if (start > end || start < 0 || end > len)
            return {nullptr};
        if (dp[start][end].size() > 0)
            return dp[start][end];
        for (int i = start; i <= end; i++)
        {
            vector<TreeNode *> left = generateTrees(start, i - 1, len, dp);
            vector<TreeNode *> right = generateTrees(i + 1, end, len, dp);
            for (auto l : left)
            {
                for (auto r : right)
                {
                    dp[start][end].push_back(new TreeNode(i + 1, l, r));
                }
            }
        }
        return dp[start][end];
    }
};

int main()
{
    cout << "Code95" << endl;
    Solution s;
    return 0;
}